package com.flower.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.flower.custom.OrderCustom;
import com.flower.mapper.IOrderDao;
import com.flower.pojo.Order;
import com.flower.pojo.OrderCodeFactory;
import com.flower.pojo.Product;
import com.flower.pojo.User;
import com.flower.pojo.request_param.CreateOrderParam;
import com.flower.pojo.request_param.EditOrderParam;
import com.flower.service.ICartService;
import com.flower.service.IOrderService;
import com.flower.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 订单模块
 */
@Controller
public class OrderController {
	
	@Resource(name = "orderService")
	private IOrderService iOrderService;
	@Resource(name = "productService")
    private IProductService iProductService;
	@Resource(name = "cartService")
	private ICartService iCartService;
	
	/**
	 *后台 —— 删除订单
	 */
	@RequestMapping("/deleteOrder.do")
	@ResponseBody
	public String delOrder(HttpServletRequest request, int oid){
		int row = iOrderService.deleteOrder(oid);
		if(row > 0){
			return "OK";
		}else{
			return "FAIL";
		}
	}
	
	/**
	 * 后台——订单展示
	 */
	@RequestMapping("/selectAllOrder.do")
	public String showOrder(@RequestParam(required=true, defaultValue="1") Integer startPage,
			Model model){
		PageHelper.startPage(startPage, 10);
		List<Order> orders = iOrderService.selectAllOrders();
		List<OrderCustom> copyOrders = this.copyOrders(orders);
		PageInfo<Order> pi = new PageInfo<>(orders);
		model.addAttribute("PAGE", pi);
		model.addAttribute("ORDERS", copyOrders);
		
		return "order_manage";
	}
	/**
	 *  拷贝订单数据
	 * @author 黄静娜
	 * @date 2019年12月15日 下午6:40:53
	 * @param orders
	 * @return
	 */
	private List<OrderCustom> copyOrders(List<Order> orders) {
		List<OrderCustom> orderCustoms = new ArrayList<OrderCustom>();
		for (Order order : orders) {
			OrderCustom orderCustom = new OrderCustom();
			BeanUtils.copyProperties(order,orderCustom);
			if(order.getOstate() == 0) {
				orderCustom.setOrderStatus("未付款");
			}
			if(order.getOstate() == 1) {
				orderCustom.setOrderStatus("已付款待发货");
			}
			if(order.getOstate() == 2) {
				orderCustom.setOrderStatus("部分发货待收货");
			}
			if(order.getOstate() == 3) {
				orderCustom.setOrderStatus("全部发货待收货");
			}
			orderCustoms.add(orderCustom);
		}
		return orderCustoms;
	}
	
	/**
	 * 创建订单
	 */
	@RequestMapping("/createOrder.do")
	@ResponseBody
	public Object createOrder(HttpServletRequest request, CreateOrderParam order){
		User user = (User) request.getSession().getAttribute("USER");
		order.setOnum(OrderCodeFactory.getOrderCode(user.getUid()));
		order.setTime(new Date());
		int row = iOrderService.insertOrder(order);
		Map<String, Object> map = new HashMap<String, Object>();
		if(row > 0){
			//创建订单之后，删除购物项，按返回按钮进入/addCart.do?num=0，进行购物项列表展示
			iCartService.deleteCartItem(order.getUid(), order.getPid());
			iOrderService.saveOrderItem(order);
			map.put("status", "OK");
			//封装业务数据
			map.put("result", order.getOnum());
			return map;
		}else{
			map.put("status", "FAIL");
			return map;
		}
	}
	/**
	 *  {
	 *  	status: OK/FALL,
	 *  	result: {}/[],
	 *  }
	 */
	
	/**
	 * 显示订单页面信息——前台
	 */
	@RequestMapping("/showOrder.do")
	public String showOrder(HttpServletRequest request, Model model){
		//获取从地址栏上传递过来的参数
		int pid = Integer.parseInt(request.getParameter("pid"));
		int pnum = Integer.parseInt(request.getParameter("pnum"));
		double subTotal = Double.parseDouble(request.getParameter("subTotal"));
		Product product = iProductService.findProductByPid(pid);
		request.getSession().setAttribute("product", product);
		request.getSession().setAttribute("pnum", pnum);
		request.getSession().setAttribute("subTotal", subTotal);
		return "redirect:/order.jsp";
	}
	
	/**
	 * 根据订单id查看订单详情
	 * @author 黄静娜
	 * @date 2019年12月15日 下午3:13:08
	 * @param orderId
	 * @return
	 */
	@RequestMapping("/showOrderDetails.do")
	public String showOrderDetails(Model model,String orderId) {
		Object result = iOrderService.showOrderDetails(orderId);
		model.addAttribute("data", result);
		return "order_details";
	}
	
	@Autowired
	private IOrderDao iOrderDao;
	/**
	 * 订单处理页面跳转
	 * @author 黄静娜
	 * @date 2019年12月15日 下午4:28:21
	 * @param model
	 * @param orderId
	 * @return
	 */
	@RequestMapping("/showEditOrder.do")
	public String showEditOrder(Model model,String orderId) {
		if(!StringUtils.isNoneBlank(orderId) || orderId.length() > 36) {
			return "WEB-INF/message.jsp";
		}
		boolean flag = iOrderDao.isexistOrderId(orderId);
		//获取购买数量
		int itemPayCount = iOrderDao.findItemPayCountByOrderId(orderId);
		//获取发货数量
		int itemSendCount = iOrderDao.findItemSendCount(orderId);
		int orderStatus =  iOrderDao.findOrderStatus(orderId);
		if(!flag) {
			return "WEB-INF/message.jsp";
		}
		model.addAttribute("orderId", orderId);
		model.addAttribute("orderStatus", orderStatus);
		model.addAttribute("itemSendCount", itemSendCount);
		model.addAttribute("itemPayCount", itemPayCount);
		return "order_edit";
	}
	/**
	 * 订单处理
	 * @author 黄静娜
	 * @date 2019年12月15日 下午4:42:15
	 * @param editOrderParam
	 * @return
	 */
	@RequestMapping("/editOrder.do")
	@ResponseBody
	public synchronized Object editOrder(EditOrderParam editOrderParam) {
		return iOrderService.editOrder(editOrderParam);
	}
	
}
